using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.Common;

namespace Normal
{
    public partial class MainScreen : Form
    {
        public MainScreen()
        {
            InitializeComponent();
        }

        private void Principal_Load(object sender, EventArgs e)
        {
            cboProvider.Items.Clear();
            cboProvider.Items.Add("System.Data.SqlClient");
            cboProvider.Items.Add("System.Data.OracleClient");
            cboProvider.Items.Add("MySql.Data.MySqlClient");
            cboProvider.Items.Add("FirebirdSql.Data.FirebirdClient");
            cboProvider.SelectedIndex = 0;
            cboConnectionString.Items.Add(Settings.Default.ConnectionString1);
            cboConnectionString.Items.Add(Settings.Default.ConnectionString2);
            cboConnectionString.Items.Add(Settings.Default.ConnectionString3);
            cboConnectionString.Items.Add(Settings.Default.ConnectionString4);
            cboConnectionString.SelectedIndex = 0;
        }

        private void btBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
            {
                txtOutputFolder.Text = folderBrowserDialog1.SelectedPath;
            }

        }

        private void Principal_FormClosing(object sender, FormClosingEventArgs e)
        {
            Settings.Default.ConnectionString1 = cboConnectionString.Items[0].ToString();
            Settings.Default.ConnectionString2 = cboConnectionString.Items[1].ToString();
            Settings.Default.ConnectionString3 = cboConnectionString.Items[2].ToString();
            Settings.Default.ConnectionString4 = cboConnectionString.Items[3].ToString();
            Settings.Default.Save();
        }

        private void cboConnectionString_Leave(object sender, EventArgs e)
        {
            if (!cboConnectionString.Items.Contains(cboConnectionString.Text) && cboConnectionString.Text != "")
            {
                cboConnectionString.Items.Insert(0, cboConnectionString.Text);
            }
        }

        private void btConnect_Click(object sender, EventArgs e)
        {
            try
            {
                DbProviderFactory f = DbProviderFactories.GetFactory(cboProvider.Text);
                DbConnection cnn = f.CreateConnection();
                cnn.ConnectionString = cboConnectionString.Text;
                cnn.Open();
                cnn.Close();
            }
            catch
            {
                MessageBox.Show("The test failed");
                return;
            }
            MessageBox.Show("Test successful");
        }

        private void chkOneFile_CheckedChanged(object sender, EventArgs e)
        {
            txtFileName.Enabled = !chkOneFile.Checked;
        }

        private void btGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                NormalDump dmp = new NormalDump(cboConnectionString.Text, cboProvider.Text);
                dmp.Generate(txtOutputFolder.Text, chkOneFile.Checked, txtFileName.Text,
                    txtNamespace.Text, chkPrefix.Checked);
                MessageBox.Show("Done!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}